import java.sql.*;
import java.util.ArrayList;

public class ContactDAO {
    // 数据库连接参数
    private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    private static final String DB_URL = "jdbc:mysql://localhost:3306/contact";
    private static final String USER = "root";
    private static final String PASS = "root123";

    // 添加联系人
    public boolean addContact(Contact contact) {
        Connection conn = null;
        Statement stmt = null;
        try {
            // 注册JDBC驱动
            Class.forName(JDBC_DRIVER);

            // 打开连接
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            // 执行插入操作
            stmt = conn.createStatement();
            String sql = "INSERT INTO contacts(name, address, phone) VALUES('" + contact.getName() + "', '" + contact.getAddress() + "', '" + contact.getPhone() + "')";
            int count = stmt.executeUpdate(sql);

            // 返回操作结果
            return count > 0;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }

        return false;
    }

    // 删除联系人
    public boolean deleteContact(String name) {
        Connection conn = null;
        Statement stmt = null;
        try {
            // 注册JDBC驱动
            Class.forName(JDBC_DRIVER);

            // 打开连接
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            // 执行删除操作
            stmt = conn.createStatement();
            String sql = "DELETE FROM contacts WHERE name='" + name + "'";
            int count = stmt.executeUpdate(sql);

            // 返回操作结果
            return count > 0;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }

        return false;
    }

    // 修改联系人信息
    public boolean updateContact(String name,Contact contact) {
        Connection conn = null;
        Statement stmt = null;
        try {
            // 注册JDBC驱动
            Class.forName(JDBC_DRIVER);

            // 打开连接
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            // 执行更新操作
            stmt = conn.createStatement();
            String sql = "UPDATE contacts SET name='"+contact.getName()+"',address='" + contact.getAddress() + "', phone='" + contact.getPhone() + "' WHERE name='" + name + "'";
            int count = stmt.executeUpdate(sql);

            // 返回操作结果
            return count > 0;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }

        return false;
    }

    // 查询联系人信息
    public ArrayList<Contact> queryContact() {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        ArrayList<Contact> contacts = new ArrayList<>();
        try {
            // 注册JDBC驱动
            Class.forName(JDBC_DRIVER);

            // 打开连接
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            // 执行查询操作
            stmt = conn.createStatement();
            String sql = "SELECT name, address, phone FROM contacts";
            rs = stmt.executeQuery(sql);

            // 处理查询结果
            while (rs.next()) {
                Contact contact = new Contact(rs.getString("name"), rs.getString("address"), rs.getString("phone"));
                contacts.add(contact);
            }

            // 返回联系人列表
            return contacts;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }

        return null;
    }
}
